#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/13
# @Author :CJX
# @File   :PH_controller.py
import serial
import time       
import binascii
import struct 


s = serial.Serial("/dev/ttyS0",9600)
def PH_controller():
	a1=0
	while (a1<=1):
		Hex_str = bytes.fromhex("0103000000044409")
		s.write(Hex_str)
		n=s.inWaiting()
		a1=a1+1
		if n:
			str1 = binascii.b2a_hex(s.read(n))
			str_A1 = str1[10:12]
			str_B1 = str1[12:14]
			str_C1 = str1[6:8]
			str_D1 = str1[8:10]
			str_ALL1 = str_D1 + str_C1 + str_B1 + str_A1
			str_F1 = str_ALL1.decode()
			Main_measured_value = struct.unpack('<f', binascii.unhexlify(str_F1.replace(' ', '')))[0]
			Main_measured_value = '%.2f' % Main_measured_value
			str_A2 = str1[18:20]
			str_B2 = str1[20:22]
			str_C2 = str1[14:16]
			str_D2 = str1[16:18]
			str_ALL2 = str_D2 + str_C2 + str_B2 + str_A2
			str_F2 = str_ALL2.decode()
			Temperature_measurement = struct.unpack('<f', binascii.unhexlify(str_F2.replace(' ', '')))[0]
			Temperature_measurement='%.2f'% Temperature_measurement
			print(Main_measured_value)
			print(Temperature_measurement)
		time.sleep(1)
	s.flushInput()
	a2 = 0
	while (a2 <= 1):
		Hex_str = bytes.fromhex("0103003000104409")
		s.write(Hex_str)
		n = s.inWaiting()
		a2 = a2 + 1
		if n:
			str1 = binascii.b2a_hex(s.read(n))
			str_A1 = str1[6:8]

			Correction_mode = str_A1

			str_A2 = str1[18:20]
			str_B2 = str1[20:22]
			str_C2 = str1[14:16]
			str_D2 = str1[16:18]
			str_ALL2 = str_D2 + str_C2 + str_B2 + str_A2
			str_F2 = str_ALL2.decode()
			calibration_target_value_1 = struct.unpack('<f', binascii.unhexlify(str_F2.replace(' ', '')))[0]
			calibration_target_value_1 = '%.2f' % calibration_target_value_1
			print(str1)
			print(Correction_mode)
			print(calibration_target_value_1)
		time.sleep(1)
	s.flushInput()


PH_controller()